package com.atguigu.lease.admin.controller.apartment;

import com.atguigu.lease.admin.service.ApartmentInfoService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.util.JwtUtil;
import com.atguigu.lease.model.bean.ApartmentInfo;
import com.atguigu.lease.model.vo.ApartmentDetailVo;
import com.atguigu.lease.model.vo.ApartmentItemVo;
import com.atguigu.lease.model.vo.ApartmentQueryVo;
import com.atguigu.lease.model.vo.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "公寓管理模块")
@RestController
@RequestMapping("/admin/apartment")
//@CrossOrigin
public class ApartmentController {

    @Autowired
    ApartmentInfoService apartmentInfoService;
    @Operation(summary = "6、根据区县id查询公寓列表")
    @GetMapping("/listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {
        //sql条件的构建器
        LambdaQueryWrapper<ApartmentInfo> queryWrapper =
                new LambdaQueryWrapper<>(ApartmentInfo.class);
        // select * from apartment_info where district_id = #{id}
        queryWrapper.eq(ApartmentInfo::getDistrictId, id);
        List<ApartmentInfo> apartmentInfos = apartmentInfoService.list(queryWrapper);
        return Result.ok(apartmentInfos);
    }

    @Operation(summary = "5、更新公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id ,
                                          @RequestParam String status) {
        apartmentInfoService.update(Wrappers.lambdaUpdate(ApartmentInfo.class)
                .eq(ApartmentInfo::getId, id)
                .set(ApartmentInfo::getIsRelease , status));
        return Result.ok();
    }
    //
    @Operation(summary = "4、删除公寓")
    @DeleteMapping("/removeById")
    public Result removeById(@RequestParam Long id) {
        apartmentInfoService.deleteApartmentById(id);
        return Result.ok();
    }


//    /admin/apartment/getDetailById?id=9
    @Operation(summary = "3、查询公寓详情")
    @GetMapping("/getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id) {
        /*
            参考接口文档：
                响应数据需要多表联查：
                    1、公寓表: AInfo
                    2、graph_info: GraphVo(name url)
                    3、label_info: LabelInfo
                    4、facility_info: FacilityInfo
                    5、fee_value+ fee_key表： 自定义类(比FeeValue多了FeeKeyName属性)
                   class ApartmemtDetail extend ApartmentInfo{
                        List<GraphVo>;
                        List<LabelInfo>;
                        List<FacilityInfo>;
                        List<FeeVaultVo>;
                   }


                业务：
                    - 根据公寓id查询公寓详情
                    - 根据公寓id查询公寓的多张图片信息(1:n)
                    - 根据公寓id查询标签、杂费、配套信息...
         */
        ApartmentDetailVo apartmentDetailVo = apartmentInfoService.getDetailById(id);
        return Result.ok(apartmentDetailVo);
    }

    ///admin/apartment/pageItem?current=1&size=10
    @Operation(summary = "2、查询公寓分页列表")
    @GetMapping("/pageItem")
    public Result<Page<ApartmentItemVo>> pageItem(@RequestParam Long current,
                                                  @RequestParam Long size,
                                                  //pojo入参：接受多个请求参数，对象不能加@RequestParam注解(这个注解只能读取一个请求参数)
                                                  ApartmentQueryVo queryVo
                                                  ) {
        Page<ApartmentItemVo> page = apartmentInfoService.getPageItem(current , size , queryVo);
        //响应数据分析： Page<ApartmentItemVo>
        //响应数据 需要查询公寓的数据 和公寓的房间信息，所以涉及多表联查 需要自定义sql
        return Result.ok(page);
    }



    /*
        1、创建接口接受请求
        2、分析如何接受请求参数
        3、处理业务
            业务判断处理
            数据库表处理
        4、分析如何响应数据给前端
     */
    @Operation(summary = "1、新增或者更新公寓")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo){
        apartmentInfoService.saveOrUpdateApartmentSubmitVo(apartmentSubmitVo);
        return Result.ok();
    }

}
